package views;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;

import business.interfaces.ICalendarioClase;
import common.Contexto;
import controllers.ClasesController;
import controllers.IErrorObserver;
import controllers.ReservasController;
import data.entities.ReservaCancha;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

/**
 * Pantalla que permite cargar los datos de una nueva clase
 * 
 */
@SuppressWarnings("serial")
public class ClasePanel extends JPanel  implements IErrorObserver {
	private JLabel nroDocL, diaL, horarioL, cantidadPorMesL;

	private JTextField nroDocT;
	
	private JSpinner cantidadPorMesS;

	private JComboBox diaC, horarioC;
	
	private JButton buscarClasesB, volverB;
	
	private String nroDoc;

	private ClasesController controller;
	
	private ClasesTablePanel clasesTablePanel;

	public ClasePanel() {
		controller = new ClasesController();
		this.controller.agregarErrorObserver(this);
		initGui();
		layoutComponents();
		initActions();
	}

	private void initGui() {
		nroDocL = new JLabel("* Nro doc.");
		String[] diasValues = new String[]{"Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo" };
		diaC = new JComboBox(diasValues);
		
		horarioL = new JLabel("* Horario");
		diaL = new JLabel("* D�a");
		cantidadPorMesL = new JLabel("* Cantidad por mes");
		
		SpinnerModel model = new SpinnerNumberModel( 1, 1, 5, 1);
		cantidadPorMesS = new JSpinner(model);
		
		Integer[] horariosValues = new Integer[24];
		for(int i = 1; i < horariosValues.length; i++){
			Integer horarioValue = new Integer(i);
			horariosValues[i-1] = horarioValue;
		}
		horarioC = new JComboBox(horariosValues);
		horarioC.setSelectedIndex(18);
			
		buscarClasesB = new JButton();
		volverB = new JButton();

		nroDocT = new JTextField(10);
	}

	private void layoutComponents() {
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST ;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(0, 2, 2, 4);
        add(nroDocL, c);

        c.gridy = GridBagConstraints.RELATIVE;
        //add(diaL, c);
        //add(horarioL, c);
        add(cantidadPorMesL,c);
        
        c.gridx = 1;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST ;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(0, 2, 2, 4);
        add(nroDocT, c);

        c.gridy = GridBagConstraints.RELATIVE;
        //add(diaC, c);
        //add(horarioC, c);
        add(cantidadPorMesS, c);
        
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(20, 0, 10, 0);
        
        
    	add(buscarClasesB, c);
        
        c.gridx = 1;
        add(volverB, c);

	}
	
	private void initActions() {
		buscarClasesB.setAction(new AbstractAction("Buscar clases"){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(validateFields()) {
					List<ICalendarioClase> clasesDisponibles = controller.getClasesDisponibles();
					clasesTablePanel = new ClasesTablePanel(clasesDisponibles, nroDocT.getText(), (Integer)cantidadPorMesS.getValue());
					clasesTablePanel.showPanel();
					clearFields();
					disposePanel();
				}else {
					notificarError("Por favor complete el numero de documento");
				}
			}			
		});
		
		volverB.setAction(new AbstractAction("Cancelar"){
			@Override
			public void actionPerformed(ActionEvent e) {
				disposePanel();
				clearFields();
			}			
		});
	}
	
	private boolean validateFields() {
		if(nroDocT.getText().length() > 0){
			nroDoc = nroDocT.getText();
			return true;
		}
		
		return false;
	}
	
	private void clearFields() {
		nroDocT.setText("");
		cantidadPorMesS.setValue((Integer)1);
		horarioC.setSelectedIndex(18);
	}
	
	//sepan disculpar la belleza de este metodo
	private Integer getDia(String diaS){
		Integer diaI = 0;
		if(diaS.equals("Lunes")){
			diaI = 1;
		} else if(diaS.equals("Martes")){
			diaI = 2;
		} else if(diaS.equals("Miercoles")){
			diaI = 3;
		}else if(diaS.equals("Jueves")){
			diaI = 4;
		}else if(diaS.equals("Viernes")){
			diaI = 5;
		}else if(diaS.equals("Sabado")){
			diaI = 6;
		}else if(diaS.equals("Domingo")){
			diaI = 7;
		}
		
		return diaI;
	}
	private void disposePanel() {
		SwingUtilities.windowForComponent(ClasePanel.this).dispose();
	}
	
	@Override
	public void notificarError(String error) {
		Contexto.displayErrorMessage(this, error);
	}
	
	public void showPanel() {
		final JFrame mainFrame = new JFrame();
        mainFrame.setTitle("Reserva de Clase");

        mainFrame.getContentPane().add(this);
        mainFrame.setPreferredSize(new Dimension(300,240));
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
	}
	
	public static void main(String[] args) {
		ClasePanel clasePanel = new ClasePanel();
		clasePanel.showPanel();
	}

	
}
